﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace MyLibary.DAL
{
    class DAL_SanPham
    {
        static DAL_Connection connect = new DAL_Connection();
        internal static List<BIZ.BIZ_SanPham> GetAll()
        {
            DataTable tb= connect.ExecuteSelect("SELECT * FROM SanPham");
            List<BIZ.BIZ_SanPham> result = new List<BIZ.BIZ_SanPham>();
            for (int i = 0; i < tb.Rows.Count; i++)
                result.Add(new BIZ.BIZ_SanPham(tb.Rows[i][0].ToString(), tb.Rows[i][1].ToString(),Convert.ToDecimal(tb.Rows[i][2].ToString()),Convert.ToDecimal( tb.Rows[i][3].ToString()), tb.Rows[i][4].ToString(), tb.Rows[i][5].ToString(), tb.Rows[i][6].ToString()));
            return result;
        }
        internal static BIZ.BIZ_SanPham GetById(string MaSanPham)
        {
            DataTable tb = connect.ExecuteSelect("SELECT * FROM SanPham WHERE MaSanPham='"+MaSanPham+"'");
            if (tb.Rows.Count < 1)
                return null;
            return new BIZ.BIZ_SanPham(tb.Rows[0][0].ToString(), tb.Rows[0][1].ToString(),Convert.ToDecimal(tb.Rows[0][2].ToString()), Convert.ToDecimal(tb.Rows[0][3].ToString()), tb.Rows[0][4].ToString(), tb.Rows[0][5].ToString(), tb.Rows[0][6].ToString());
        }
        internal static bool Insert(BIZ.BIZ_SanPham sanPham)
        {
            string query=String.Format("INSERT INTO SanPham VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}')",sanPham.MaSanPham,sanPham.TenSanPham,sanPham.GiaNhap,sanPham.GiaBan,sanPham.MoTa,sanPham.LinkAnh,sanPham.MaLoai);
            return connect.ExecuteUpdate(query);
        }
        internal static bool Update(BIZ.BIZ_SanPham sanPham)
        {
            string query = String.Format("Update SanPham SET MaSanPham='{0}',TenSanPham='{1}',GiaNhap='{2}',GiaBan='{3}',MoTa='{4}',LinkAnh='{5}',MaLoai='{6}'", sanPham.MaSanPham, sanPham.TenSanPham, sanPham.GiaNhap,sanPham.GiaBan, sanPham.MoTa, sanPham.LinkAnh, sanPham.MaLoai);
            return connect.ExecuteUpdate(query);
        }
        internal static bool Delete(BIZ.BIZ_SanPham sanPham)
        {
            return connect.ExecuteUpdate("DELETE SanPham WHERE MaSanPham='"+sanPham.MaSanPham+"'");
        }
    }
}
